
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" lang="zh_Hans">
  <head>
    <meta http-equiv="X-UA-Compatible" content="IE=Edge" />
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Django 配置 &#8212; Django 3.2.11.dev 文档</title>
    <link rel="stylesheet" href="../_static/default.css" type="text/css" />
    <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
    <script type="text/javascript" id="documentation_options" data-url_root="../" src="../_static/documentation_options.js"></script>
    <script type="text/javascript" src="../_static/jquery.js"></script>
    <script type="text/javascript" src="../_static/underscore.js"></script>
    <script type="text/javascript" src="../_static/doctools.js"></script>
    <script type="text/javascript" src="../_static/language_data.js"></script>
    <link rel="index" title="索引" href="../genindex.html" />
    <link rel="search" title="搜索" href="../search.html" />
    <link rel="next" title="信号" href="signals.html" />
    <link rel="prev" title="序列化 Django 对象" href="serialization.html" />



 
<script src="../templatebuiltins.js"></script>
<script>
(function($) {
    if (!django_template_builtins) {
       // templatebuiltins.js missing, do nothing.
       return;
    }
    $(document).ready(function() {
        // Hyperlink Django template tags and filters
        var base = "../ref/templates/builtins.html";
        if (base == "#") {
            // Special case for builtins.html itself
            base = "";
        }
        // Tags are keywords, class '.k'
        $("div.highlight\\-html\\+django span.k").each(function(i, elem) {
             var tagname = $(elem).text();
             if ($.inArray(tagname, django_template_builtins.ttags) != -1) {
                 var fragment = tagname.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + tagname + "</a>");
             }
        });
        // Filters are functions, class '.nf'
        $("div.highlight\\-html\\+django span.nf").each(function(i, elem) {
             var filtername = $(elem).text();
             if ($.inArray(filtername, django_template_builtins.tfilters) != -1) {
                 var fragment = filtername.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + filtername + "</a>");
             }
        });
    });
})(jQuery);</script>

  </head><body>

    <div class="document">
  <div id="custom-doc" class="yui-t6">
    <div id="hd">
      <h1><a href="../index.html">Django 3.2.11.dev 文档</a></h1>
      <div id="global-nav">
        <a title="Home page" href="../index.html">Home</a>  |
        <a title="Table of contents" href="../contents.html">Table of contents</a>  |
        <a title="Global index" href="../genindex.html">Index</a>  |
        <a title="Module index" href="../py-modindex.html">Modules</a>
      </div>
      <div class="nav">
    &laquo; <a href="serialization.html" title="序列化 Django 对象">previous</a>
     |
    <a href="index.html" title="使用 Django" accesskey="U">up</a>
   |
    <a href="signals.html" title="信号">next</a> &raquo;</div>
    </div>

    <div id="bd">
      <div id="yui-main">
        <div class="yui-b">
          <div class="yui-g" id="topics-settings">
            
  <div class="section" id="s-django-settings">
<span id="django-settings"></span><h1>Django 配置<a class="headerlink" href="#django-settings" title="永久链接至标题">¶</a></h1>
<p>Django 的配置文件包含 Django 应用的所有配置项。本文档介绍配置是如何生效的，以及哪些设置项是可配置的。</p>
<div class="section" id="s-the-basics">
<span id="the-basics"></span><h2>基础<a class="headerlink" href="#the-basics" title="永久链接至标题">¶</a></h2>
<p>配置文件只是一个使用模块级变量的一个 Python 模块。</p>
<p>一组配置例子:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">ALLOWED_HOSTS</span> <span class="o">=</span> <span class="p">[</span><span class="s1">&#39;www.example.com&#39;</span><span class="p">]</span>
<span class="n">DEBUG</span> <span class="o">=</span> <span class="kc">False</span>
<span class="n">DEFAULT_FROM_EMAIL</span> <span class="o">=</span> <span class="s1">&#39;webmaster@example.com&#39;</span>
</pre></div>
</div>
<div class="admonition note">
<p class="first admonition-title">注解</p>
<p class="last">如果你将 <a class="reference internal" href="../ref/settings.html#std:setting-DEBUG"><code class="xref std std-setting docutils literal notranslate"><span class="pre">DEBUG</span></code></a> 设置为 <code class="docutils literal notranslate"><span class="pre">False</span></code>，同时你需要正确的设置 <a class="reference internal" href="../ref/settings.html#std:setting-ALLOWED_HOSTS"><code class="xref std std-setting docutils literal notranslate"><span class="pre">ALLOWED_HOSTS</span></code></a>。</p>
</div>
<p>因为配置文件是一个 Python 模块，所以要注意以下几项:</p>
<ul>
<li><p class="first">不能有 Python 语法错误</p>
</li>
<li><p class="first">可以用 Python 语法实现动态配置，例如:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">MY_SETTING</span> <span class="o">=</span> <span class="p">[</span><span class="nb">str</span><span class="p">(</span><span class="n">i</span><span class="p">)</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">30</span><span class="p">)]</span>
</pre></div>
</div>
</li>
<li><p class="first">可以从其他配置文件中引入变量</p>
</li>
</ul>
</div>
<div class="section" id="s-designating-the-settings">
<span id="s-django-settings-module"></span><span id="designating-the-settings"></span><span id="django-settings-module"></span><h2>指定配置文件<a class="headerlink" href="#designating-the-settings" title="永久链接至标题">¶</a></h2>
<dl class="envvar">
<dt id="envvar-DJANGO_SETTINGS_MODULE">
<code class="descname">DJANGO_SETTINGS_MODULE</code><a class="headerlink" href="#envvar-DJANGO_SETTINGS_MODULE" title="永久链接至目标">¶</a></dt>
<dd></dd></dl>

<p>当你使用 Django 的时候，你必须告诉它你正在使用哪些设置。通过使用环境变量 <span class="target" id="index-20"></span><a class="reference internal" href="#envvar-DJANGO_SETTINGS_MODULE"><code class="xref std std-envvar docutils literal notranslate"><span class="pre">DJANGO_SETTINGS_MODULE</span></code></a> 来实现。</p>
<p><span class="target" id="index-21"></span><a class="reference internal" href="#envvar-DJANGO_SETTINGS_MODULE"><code class="xref std std-envvar docutils literal notranslate"><span class="pre">DJANGO_SETTINGS_MODULE</span></code></a> 的值是一个符合 Python 语法的路径，例如 <code class="docutils literal notranslate"><span class="pre">mysite.settings</span></code>。要注意配置模块应位于 Python 的 <a class="reference external" href="https://diveinto.org/python3/your-first-python-program.html#importsearchpath">import 搜索路径</a> 中。</p>
<div class="section" id="s-the-django-admin-utility">
<span id="the-django-admin-utility"></span><h3><code class="docutils literal notranslate"><span class="pre">django-admin</span></code> 工具<a class="headerlink" href="#the-django-admin-utility" title="永久链接至标题">¶</a></h3>
<p>使用 <a class="reference internal" href="../ref/django-admin.html"><span class="doc">django-admin</span></a> 时，你可以一次性设定环境变量，也可以在运行此工具时，显式地传入配置模块。</p>
<p>例子（Unix Bash shell）:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">export</span> <span class="n">DJANGO_SETTINGS_MODULE</span><span class="o">=</span><span class="n">mysite</span><span class="o">.</span><span class="n">settings</span>
<span class="n">django</span><span class="o">-</span><span class="n">admin</span> <span class="n">runserver</span>
</pre></div>
</div>
<p>例子（Windows shell）:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="nb">set</span> <span class="n">DJANGO_SETTINGS_MODULE</span><span class="o">=</span><span class="n">mysite</span><span class="o">.</span><span class="n">settings</span>
<span class="n">django</span><span class="o">-</span><span class="n">admin</span> <span class="n">runserver</span>
</pre></div>
</div>
<p>使用 <code class="docutils literal notranslate"><span class="pre">--settings</span></code> 命令行参数指定配置模块路径:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">django</span><span class="o">-</span><span class="n">admin</span> <span class="n">runserver</span> <span class="o">--</span><span class="n">settings</span><span class="o">=</span><span class="n">mysite</span><span class="o">.</span><span class="n">settings</span>
</pre></div>
</div>
</div>
<div class="section" id="s-on-the-server-mod-wsgi">
<span id="on-the-server-mod-wsgi"></span><h3>服务器端 (<code class="docutils literal notranslate"><span class="pre">mode_wsgi</span></code>)<a class="headerlink" href="#on-the-server-mod-wsgi" title="永久链接至标题">¶</a></h3>
<p>在服务器环境下，要通知WSGI应用当前使用的是哪个配置文件，用到 <code class="docutils literal notranslate"><span class="pre">os.environ</span></code>:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">os</span>

<span class="n">os</span><span class="o">.</span><span class="n">environ</span><span class="p">[</span><span class="s1">&#39;DJANGO_SETTINGS_MODULE&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="s1">&#39;mysite.settings&#39;</span>
</pre></div>
</div>
<p>阅读 <a class="reference internal" href="../howto/deployment/wsgi/modwsgi.html"><span class="doc">Django mod_wsgi 文档</span></a> 了解更多关于 Django WSGI 应用的信息和常见设置。</p>
</div>
</div>
<div class="section" id="s-default-settings">
<span id="default-settings"></span><h2>默认配置<a class="headerlink" href="#default-settings" title="永久链接至标题">¶</a></h2>
<p>在无需配置的情况下，Django配置文件没必要定义任何配置项。因为每个配置项都有一个明确的默认值。默认值都保存在模块 <code class="file docutils literal notranslate"><span class="pre">django/conf/global_settings.py</span></code> 中。</p>
<p>Django按如下算法编译配置模块:</p>
<ul class="simple">
<li>从 <code class="docutils literal notranslate"><span class="pre">global_settings.py</span></code> 中加载配置项。</li>
<li>从指定的配置文件中加载配置项，覆盖对应的全局配置项。</li>
</ul>
<p>注意: 配置文件中不要再重复引用 <code class="docutils literal notranslate"><span class="pre">global_settings</span></code>，因为这样做是多余的。</p>
<div class="section" id="s-seeing-which-settings-you-ve-changed">
<span id="seeing-which-settings-you-ve-changed"></span><h3>查看你已经更改了哪个配置项<a class="headerlink" href="#seeing-which-settings-you-ve-changed" title="永久链接至标题">¶</a></h3>
<p>命令 <code class="docutils literal notranslate"><span class="pre">python</span> <span class="pre">manage.py</span> <span class="pre">diffsettings</span></code> 显示当前配置文件与 Django 默认配置的差异。</p>
<p>想了解更多, 可以查看 <a class="reference internal" href="../ref/django-admin.html#django-admin-diffsettings"><code class="xref std std-djadmin docutils literal notranslate"><span class="pre">diffsettings</span></code></a> 文档.</p>
</div>
</div>
<div class="section" id="s-using-settings-in-python-code">
<span id="using-settings-in-python-code"></span><h2>在 Python 代码中使用 settings<a class="headerlink" href="#using-settings-in-python-code" title="永久链接至标题">¶</a></h2>
<p>在具体的Django应用中, 通过引入 <code class="docutils literal notranslate"><span class="pre">django.conf.settings</span></code> 使用配置, 例:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.conf</span> <span class="kn">import</span> <span class="n">settings</span>

<span class="k">if</span> <span class="n">settings</span><span class="o">.</span><span class="n">DEBUG</span><span class="p">:</span>
    <span class="c1"># Do something</span>
</pre></div>
</div>
<p>注意 <code class="docutils literal notranslate"><span class="pre">django.conf.settings</span></code> 并不是一个模块！-- 而是一个对象！所以引入某个单独的设置项是不可能的:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.conf.settings</span> <span class="kn">import</span> <span class="n">DEBUG</span>  <span class="c1"># This won&#39;t work.</span>
</pre></div>
</div>
<p>还要注意 <em>不应该</em> 直接引入 <code class="docutils literal notranslate"><span class="pre">global_settings</span></code> 和你自己的配置文件。 <code class="docutils literal notranslate"><span class="pre">django.conf.settings</span></code> 整合了默认配置和网站指定配置，从而只提供一个接口读取配置。它也解耦了使用自定义配置文件的代码。</p>
</div>
<div class="section" id="s-altering-settings-at-runtime">
<span id="altering-settings-at-runtime"></span><h2>运行时更改设置<a class="headerlink" href="#altering-settings-at-runtime" title="永久链接至标题">¶</a></h2>
<p>不建议在运行时更改设置. 比如, 不要在view中这样用:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.conf</span> <span class="kn">import</span> <span class="n">settings</span>

<span class="n">settings</span><span class="o">.</span><span class="n">DEBUG</span> <span class="o">=</span> <span class="kc">True</span>   <span class="c1"># Don&#39;t do this!</span>
</pre></div>
</div>
<p>应该只在 settings 文件中更改设置。</p>
</div>
<div class="section" id="s-security">
<span id="security"></span><h2>安全<a class="headerlink" href="#security" title="永久链接至标题">¶</a></h2>
<p>由于配置文件包含敏感信息, 比如数据库密码, 所以应该限制对配置文件的访问. 比如更改文件权限, 从而只能令你和你的WEB服务器能读取配置文件. 这在共享主机的环境中非常重要!</p>
</div>
<div class="section" id="s-available-settings">
<span id="available-settings"></span><h2>可用的配置项<a class="headerlink" href="#available-settings" title="永久链接至标题">¶</a></h2>
<p>查看 <a class="reference internal" href="../ref/settings.html"><span class="doc">settings 参考</span></a> 了解完整的配置项列表。</p>
</div>
<div class="section" id="s-creating-your-own-settings">
<span id="creating-your-own-settings"></span><h2>创建你自己的配置文件<a class="headerlink" href="#creating-your-own-settings" title="永久链接至标题">¶</a></h2>
<p>没有什么可以阻止你为你的的 Django 应用创建自己的配置，但请遵循以下原则：</p>
<ul class="simple">
<li>所有配置项名称都应大写。</li>
<li>不要重新设置已存在的配置项。</li>
</ul>
<p>由于配置文件是序列化的，Django 使用列表，但仅是约定俗成。</p>
</div>
<div class="section" id="s-using-settings-without-setting-django-settings-module">
<span id="s-settings-without-django-settings-module"></span><span id="using-settings-without-setting-django-settings-module"></span><span id="settings-without-django-settings-module"></span><h2>不设置 <span class="target" id="index-12"></span><a class="reference internal" href="#envvar-DJANGO_SETTINGS_MODULE"><code class="xref std std-envvar docutils literal notranslate"><span class="pre">DJANGO_SETTINGS_MODULE</span></code></a> 时使用配置<a class="headerlink" href="#using-settings-without-setting-django-settings-module" title="永久链接至标题">¶</a></h2>
<p>在某些情况下，你可能希望绕过：<span class="target" id="index-22"></span><a class="reference internal" href="#envvar-DJANGO_SETTINGS_MODULE"><code class="xref std std-envvar docutils literal notranslate"><span class="pre">DJANGO_SETTINGS_MODULE</span></code></a> 环境变量。例如，如果你单独使用模板系统，则可能不需要设置指向配置模块的环境变量。</p>
<p>在这种情况下，可以手动设定 Django 的配置项。要用到下面这个方法:</p>
<dl class="function">
<dt id="django.conf.settings.configure">
<code class="descclassname">django.conf.settings.</code><code class="descname">configure</code>(<em>default_settings</em>, <em>**settings</em>)<a class="headerlink" href="#django.conf.settings.configure" title="永久链接至目标">¶</a></dt>
<dd></dd></dl>

<p>举例：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.conf</span> <span class="kn">import</span> <span class="n">settings</span>

<span class="n">settings</span><span class="o">.</span><span class="n">configure</span><span class="p">(</span><span class="n">DEBUG</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
</pre></div>
</div>
<p>通过 <code class="docutils literal notranslate"><span class="pre">configure()</span></code> 可以设置任何配置项，每个参数对应一个值。参数名称必须大写，而且参数名必须是真实存在。没有在 <code class="docutils literal notranslate"><span class="pre">configure()</span></code> 中出现的设置项在随后用到时，仍会使用默认值。</p>
<p>如果你并不想使用整个框架，仅仅是其中一小部分时，如上述这般配置 Django 是非常必要的。事实上，我们建议您这样做。</p>
<p>因此，通过 <code class="docutils literal notranslate"><span class="pre">settings.configure()</span></code> 配置 Django 时，并不会对当前进程的环境变量做任何修改。（可以查看 <a class="reference internal" href="../ref/settings.html#std:setting-TIME_ZONE"><code class="xref std std-setting docutils literal notranslate"><span class="pre">TIME_ZONE</span></code></a> 了解为什么要这么做）。这意味着在上述情况下你仍然完全掌控着当前环境。</p>
<div class="section" id="s-custom-default-settings">
<span id="custom-default-settings"></span><h3>定制默认配置项<a class="headerlink" href="#custom-default-settings" title="永久链接至标题">¶</a></h3>
<p>如果你实在不想使用 <code class="docutils literal notranslate"><span class="pre">django.conf.global_settings</span></code>，而想自定义配置默认值，你可以在调用 <code class="docutils literal notranslate"><span class="pre">configure()</span></code> 时使用 <code class="docutils literal notranslate"><span class="pre">default_settings</span></code> 参数（或是默认做为第一个参数传入）。该参数是一个提供默认值的模块或是类。</p>
<p>在下面这个例子中，由 <code class="docutils literal notranslate"><span class="pre">myapp_defaults</span></code> 提供默认值，并令 <a class="reference internal" href="../ref/settings.html#std:setting-DEBUG"><code class="xref std std-setting docutils literal notranslate"><span class="pre">DEBUG</span></code></a> 被置为 <code class="docutils literal notranslate"><span class="pre">True</span></code> 而不是从 <code class="docutils literal notranslate"><span class="pre">myapp_defaults</span></code> 中获取:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.conf</span> <span class="kn">import</span> <span class="n">settings</span>
<span class="kn">from</span> <span class="nn">myapp</span> <span class="kn">import</span> <span class="n">myapp_defaults</span>

<span class="n">settings</span><span class="o">.</span><span class="n">configure</span><span class="p">(</span><span class="n">default_settings</span><span class="o">=</span><span class="n">myapp_defaults</span><span class="p">,</span> <span class="n">DEBUG</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
</pre></div>
</div>
<p>接下来例子使用 <code class="docutils literal notranslate"><span class="pre">myapp_defaults</span></code> 做为一个位置参数，这和上面的效果是等同的:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">settings</span><span class="o">.</span><span class="n">configure</span><span class="p">(</span><span class="n">myapp_defaults</span><span class="p">,</span> <span class="n">DEBUG</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
</pre></div>
</div>
<p>一般来说，没必要自行设置默认值。Django 的默认设置是比较宽松的，你可以放心地使用。要注意的是，如果你传入了新的默认模块，那它就会完全 <em>替换</em> Django 本身的默认设置。因此，你必须为接下来可能会用到的所有设置项都重新指定默认值。查看 <code class="docutils literal notranslate"><span class="pre">django.conf.settings.global_settings</span></code> 了解全部默认值。</p>
</div>
<div class="section" id="s-either-configure-or-django-settings-module-is-required">
<span id="either-configure-or-django-settings-module-is-required"></span><h3><code class="docutils literal notranslate"><span class="pre">configure()</span></code> 或 <span class="target" id="index-14"></span><a class="reference internal" href="#envvar-DJANGO_SETTINGS_MODULE"><code class="xref std std-envvar docutils literal notranslate"><span class="pre">DJANGO_SETTINGS_MODULE</span></code></a> 二者必用其一<a class="headerlink" href="#either-configure-or-django-settings-module-is-required" title="永久链接至标题">¶</a></h3>
<p>如果你没有设置  <span class="target" id="index-23"></span><a class="reference internal" href="#envvar-DJANGO_SETTINGS_MODULE"><code class="xref std std-envvar docutils literal notranslate"><span class="pre">DJANGO_SETTINGS_MODULE</span></code></a> 环境变量，就 <em>必须</em> 在运行读取配置项的代码之前调用 <code class="docutils literal notranslate"><span class="pre">configure()</span></code>。</p>
<p>如果你没有设置 <span class="target" id="index-24"></span><a class="reference internal" href="#envvar-DJANGO_SETTINGS_MODULE"><code class="xref std std-envvar docutils literal notranslate"><span class="pre">DJANGO_SETTINGS_MODULE</span></code></a> 也没有调用 <code class="docutils literal notranslate"><span class="pre">configure()</span></code>，那么 Django 会在第一次访问配置项时抛出 <code class="docutils literal notranslate"><span class="pre">ImportError</span></code> 异常。</p>
<p>如果设置了 <span class="target" id="index-25"></span><a class="reference internal" href="#envvar-DJANGO_SETTINGS_MODULE"><code class="xref std std-envvar docutils literal notranslate"><span class="pre">DJANGO_SETTINGS_MODULE</span></code></a>，以某种方式访问配置的值，<em>再</em> 调用 <code class="docutils literal notranslate"><span class="pre">configure()</span></code> 的话，Django 将会抛出一个 <code class="docutils literal notranslate"><span class="pre">RuntimeError</span></code> 异常，表明已经设置了配置。</p>
<p>例如:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.conf</span> <span class="kn">import</span> <span class="n">settings</span>
<span class="k">if</span> <span class="ow">not</span> <span class="n">settings</span><span class="o">.</span><span class="n">configured</span><span class="p">:</span>
    <span class="n">settings</span><span class="o">.</span><span class="n">configure</span><span class="p">(</span><span class="n">myapp_defaults</span><span class="p">,</span> <span class="n">DEBUG</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
</pre></div>
</div>
<p>重复调用 <code class="docutils literal notranslate"><span class="pre">configure()</span></code> 或是在访问任何一个配置项后再调用 <code class="docutils literal notranslate"><span class="pre">configure()</span></code>，也会引起错误。</p>
<p>这意味着：确保要使用 <code class="docutils literal notranslate"><span class="pre">configure()</span></code> 或 <span class="target" id="index-26"></span><a class="reference internal" href="#envvar-DJANGO_SETTINGS_MODULE"><code class="xref std std-envvar docutils literal notranslate"><span class="pre">DJANGO_SETTINGS_MODULE</span></code></a> 其中一个，不要多，也不要少。</p>
</div>
<div class="section" id="s-calling-django-setup-is-required-for-standalone-django-usage">
<span id="calling-django-setup-is-required-for-standalone-django-usage"></span><h3>“独立的” Django 应用要求调用 <code class="docutils literal notranslate"><span class="pre">django.setup()</span></code><a class="headerlink" href="#calling-django-setup-is-required-for-standalone-django-usage" title="永久链接至标题">¶</a></h3>
<p>如果你使用了“独立” Django 的组件——比如，编写一个 Python 脚本，它加载了一些 Django 模板并渲染了它们，或使用 ORM 获取数据——你需要额外的配置一下设置文件。</p>
<p>在你已经配置 <span class="target" id="index-27"></span><a class="reference internal" href="#envvar-DJANGO_SETTINGS_MODULE"><code class="xref std std-envvar docutils literal notranslate"><span class="pre">DJANGO_SETTINGS_MODULE</span></code></a> 或调用 <code class="docutils literal notranslate"><span class="pre">configure()</span></code> 后，你需要调用 <a class="reference internal" href="../ref/applications.html#django.setup" title="django.setup"><code class="xref py py-func docutils literal notranslate"><span class="pre">django.setup()</span></code></a> 来加载你的设置，并填写的 Django 的应用注册表。例子:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">django</span>
<span class="kn">from</span> <span class="nn">django.conf</span> <span class="kn">import</span> <span class="n">settings</span>
<span class="kn">from</span> <span class="nn">myapp</span> <span class="kn">import</span> <span class="n">myapp_defaults</span>

<span class="n">settings</span><span class="o">.</span><span class="n">configure</span><span class="p">(</span><span class="n">default_settings</span><span class="o">=</span><span class="n">myapp_defaults</span><span class="p">,</span> <span class="n">DEBUG</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
<span class="n">django</span><span class="o">.</span><span class="n">setup</span><span class="p">()</span>

<span class="c1"># Now this script or any imported module can use any part of Django it needs.</span>
<span class="kn">from</span> <span class="nn">myapp</span> <span class="kn">import</span> <span class="n">models</span>
</pre></div>
</div>
<p>注意，仅在你的代码是真的独立的情况下调用 <code class="docutils literal notranslate"><span class="pre">django.setup()</span></code>。当被你的 Web 服务器调用时，或通过 <a class="reference internal" href="../ref/django-admin.html"><span class="doc">django-admin</span></a> 调用，Django 会帮你处理这些。</p>
<div class="admonition-django-setup-may-only-be-called-once admonition">
<p class="first admonition-title"><code class="docutils literal notranslate"><span class="pre">django.setup()</span></code> 只需调用一次。</p>
<p>因此，避免将可服用的应用逻辑代码放入独立脚本中。是故，你必须在你的应用中从此脚本中导入。若你无法避免这样做，将 <code class="docutils literal notranslate"><span class="pre">django.setup()</span></code> 调用至于 <code class="docutils literal notranslate"><span class="pre">if</span></code> 块内:</p>
<div class="last highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">if</span> <span class="vm">__name__</span> <span class="o">==</span> <span class="s1">&#39;__main__&#39;</span><span class="p">:</span>
    <span class="kn">import</span> <span class="nn">django</span>
    <span class="n">django</span><span class="o">.</span><span class="n">setup</span><span class="p">()</span>
</pre></div>
</div>
</div>
<div class="admonition seealso">
<p class="first admonition-title">参见</p>
<dl class="last docutils">
<dt><a class="reference internal" href="../ref/settings.html"><span class="doc">配置参考</span></a></dt>
<dd>包含对于 core 和 contrib 的完整应用配置列表。</dd>
</dl>
</div>
</div>
</div>
</div>


          </div>
        </div>
      </div>
      
        
          <div class="yui-b" id="sidebar">
            
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
  <h3><a href="../contents.html">Table of Contents</a></h3>
  <ul>
<li><a class="reference internal" href="#">Django 配置</a><ul>
<li><a class="reference internal" href="#the-basics">基础</a></li>
<li><a class="reference internal" href="#designating-the-settings">指定配置文件</a><ul>
<li><a class="reference internal" href="#the-django-admin-utility"><code class="docutils literal notranslate"><span class="pre">django-admin</span></code> 工具</a></li>
<li><a class="reference internal" href="#on-the-server-mod-wsgi">服务器端 (<code class="docutils literal notranslate"><span class="pre">mode_wsgi</span></code>)</a></li>
</ul>
</li>
<li><a class="reference internal" href="#default-settings">默认配置</a><ul>
<li><a class="reference internal" href="#seeing-which-settings-you-ve-changed">查看你已经更改了哪个配置项</a></li>
</ul>
</li>
<li><a class="reference internal" href="#using-settings-in-python-code">在 Python 代码中使用 settings</a></li>
<li><a class="reference internal" href="#altering-settings-at-runtime">运行时更改设置</a></li>
<li><a class="reference internal" href="#security">安全</a></li>
<li><a class="reference internal" href="#available-settings">可用的配置项</a></li>
<li><a class="reference internal" href="#creating-your-own-settings">创建你自己的配置文件</a></li>
<li><a class="reference internal" href="#using-settings-without-setting-django-settings-module">不设置 <code class="docutils literal notranslate"><span class="pre">DJANGO_SETTINGS_MODULE</span></code> 时使用配置</a><ul>
<li><a class="reference internal" href="#custom-default-settings">定制默认配置项</a></li>
<li><a class="reference internal" href="#either-configure-or-django-settings-module-is-required"><code class="docutils literal notranslate"><span class="pre">configure()</span></code> 或 <code class="docutils literal notranslate"><span class="pre">DJANGO_SETTINGS_MODULE</span></code> 二者必用其一</a></li>
<li><a class="reference internal" href="#calling-django-setup-is-required-for-standalone-django-usage">“独立的” Django 应用要求调用 <code class="docutils literal notranslate"><span class="pre">django.setup()</span></code></a></li>
</ul>
</li>
</ul>
</li>
</ul>

  <h4>上一个主题</h4>
  <p class="topless"><a href="serialization.html"
                        title="上一章">序列化 Django 对象</a></p>
  <h4>下一个主题</h4>
  <p class="topless"><a href="signals.html"
                        title="下一章">信号</a></p>
  <div role="note" aria-label="source link">
    <h3>本页</h3>
    <ul class="this-page-menu">
      <li><a href="../_sources/topics/settings.txt"
            rel="nofollow">显示源代码</a></li>
    </ul>
   </div>
<div id="searchbox" style="display: none" role="search">
  <h3>快速搜索</h3>
    <div class="searchformwrapper">
    <form class="search" action="../search.html" method="get">
      <input type="text" name="q" />
      <input type="submit" value="转向" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    </div>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
              <h3>Last update:</h3>
              <p class="topless">12月 07, 2021</p>
          </div>
        
      
    </div>

    <div id="ft">
      <div class="nav">
    &laquo; <a href="serialization.html" title="序列化 Django 对象">previous</a>
     |
    <a href="index.html" title="使用 Django" accesskey="U">up</a>
   |
    <a href="signals.html" title="信号">next</a> &raquo;</div>
    </div>
  </div>

      <div class="clearer"></div>
    </div>
  </body>
</html>